package com.ssy.lingxi.purchase.model.response;

import cn.hutool.core.util.ObjectUtil;
import com.ssy.lingxi.purchase.entity.PurchaseRequisition;
import com.ssy.lingxi.purchase.model.bo.FileUploadBO;
import com.ssy.lingxi.purchase.model.enums.DeliveryMethodEnum;
import com.ssy.lingxi.purchase.model.enums.DeliveryTypeEnum;
import com.ssy.lingxi.purchase.model.enums.PurchaseConstants;
import com.ssy.lingxi.purchase.model.enums.RequisitionInnerStatusEnum;
import com.ssy.lingxi.purchase.model.vo.FileUploadVO;
import com.ssy.lingxi.purchase.utils.NumberUtil;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wyx
 * @version 2.0.0
 * @date 2021-10-29
 */
@Setter
@Getter
public class RequisitionPageRes {
    /**
     * 请购单Id
     */
    private Long id;

    /**
     * 请购单编号
     */
    private String requisitionNo;

    /**
     * 摘要
     */
    private String digest;

    /**
     * 卖方（供应）会员Id
     */
    private Long vendorMemberId;

    /**
     * 卖方（供应）会员角色Id
     */
    private Long vendorRoleId;

    /**
     * 卖方（供应）会员名称
     */
    private String vendorMemberName;

    /**
     * 订单预交日期，格式为yyyy-MM-dd
     */
    private String deliverTime;

    /**
     * 部门
     */
    private String department;

    /**
     * 用途
     */
    private String purpose;

    /**
     * 请购数量
     */
    private String quantity;

    /**
     * 已转订单数量
     */
    private String transferQuantity;

    /**
     * 创建时间（下单时间）
     */
    private String createTime;

    /**
     * 内部状态
     */
    private Integer innerStatus;

    /**
     * 内部状态名称
     */
    private String innerStatusName;

    /**
     * 是否显示取消按钮
     */
    private Boolean showCancel;

    /**
     * 是否显示中止按钮
     */
    private Boolean showPause;

    /**
     * 是否显示提交审核按钮
     */
    private Boolean showSubmit;

    /**
     * 是否显示删除按钮
     */
    private Boolean showDelete;

    /**
     * 预交日期
     */
    private String advanceDeliveryDate;

    /**
     * 配送方式枚举
     *    1、供应商直发客户
     *    2、供应商配送
     *    3、自提
     *    4、客户自提
     */
    private Integer deliveryMethod;

    /**
     * 配送方式名称
     */
    private String deliveryMethodName;

    /**
     * 直送客户/客户自提:
     *   1、直送客户
     *   2、客户自提
     */
    private Integer deliveryType;

    /**
     * 直送客户/客户自提名称
     */
    private String deliveryTypeName;

    /**
     * 送货地址id
     */
    private Long deliveryAddressId;

    /**
     * 送货地址
     */
    private String deliveryAddress;

    /**
     * 请购人ID
     */
    private Long requisitionerId;

    /**
     * 请购人名称
     */
    private String requisitioner;

    /**
     * 附件
     */
    private List<FileUploadVO> attachments;

    public RequisitionPageRes(PurchaseRequisition requisition) {
        this.id = requisition.getId();
        this.requisitionNo = requisition.getRequisitionNo();
        this.digest = requisition.getDigest();
        this.vendorMemberId = requisition.getVendorMemberId();
        this.vendorRoleId = requisition.getVendorRoleId();
        this.vendorMemberName = requisition.getVendorMemberName();
        this.deliverTime = requisition.getDeliverTime() == null ? "" : requisition.getDeliverTime().format(PurchaseConstants.DEFAULT_DATE_FORMATTER);
        this.department = requisition.getDepartment();
        this.purpose = requisition.getPurpose();
        this.quantity = NumberUtil.formatToInteger(requisition.getQuantity());
        this.transferQuantity = NumberUtil.formatToInteger(requisition.getTransferQuantity());
        this.createTime = requisition.getCreateTime().format(PurchaseConstants.DEFAULT_TIME_FORMATTER);
        this.innerStatus = requisition.getBuyerInnerStatus();
        this.innerStatusName = RequisitionInnerStatusEnum.getNameByCode(requisition.getBuyerInnerStatus());
        this.showCancel = RequisitionInnerStatusEnum.BUYER__SUBMITTED.getCode().equals(requisition.getBuyerInnerStatus()) && requisition.getTransferQuantity().compareTo(BigDecimal.ZERO) <= 0;
        this.showPause = RequisitionInnerStatusEnum.BUYER__SUBMITTED.getCode().equals(requisition.getBuyerInnerStatus()) && requisition.getTransferQuantity().compareTo(requisition.getQuantity()) < 0;
        this.showSubmit = RequisitionInnerStatusEnum.BUYER_TO_SUBMIT_VALIDATE.getCode().equals(requisition.getBuyerInnerStatus());
        this.showDelete = RequisitionInnerStatusEnum.BUYER_TO_SUBMIT_VALIDATE.getCode().equals(requisition.getBuyerInnerStatus());
        this.advanceDeliveryDate = requisition.getAdvanceDeliveryDate();
        this.deliveryMethod = requisition.getDeliveryMethod();
        this.deliveryMethodName = DeliveryMethodEnum.getCodeMsg(NumberUtil.isNullOrZero(requisition.getDeliveryMethod()) ? 0 : requisition.getDeliveryMethod());
        this.deliveryType = requisition.getDeliveryType();
        this.deliveryTypeName = DeliveryTypeEnum.getCodeMsg(NumberUtil.isNullOrZero(requisition.getDeliveryType()) ? 0 : requisition.getDeliveryType());
        this.deliveryAddress = requisition.getDeliveryAddress();
        this.deliveryAddressId = requisition.getDeliveryAddressId();
        this.requisitionerId = requisition.getRequisitionerId();
        this.requisitioner = requisition.getRequisitioner();
        this.attachments = requisition.getAttachments();
    }
}
